package figure8.GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import figure8.Controller;
import figure8.LoopTiming;
import figure8.Project;

/**
 * This class is the panel for containing the main GUI relating to the current
 * project. It is composed of a TimingPanel for displaying the timing of the
 * project's loop and a TrackList in a scrolling pane to displaying all the
 * tracks in the project.
 */
public class ProjectPanel extends JPanel {
	private static final long serialVersionUID = -1219322788922037434L;
	private TrackList list;
	private TimingPanel temp = null;

	private Controller controller;

	public ProjectPanel(Controller controller) {
		super();

		this.controller = controller;

		// Use a border layout with the timing panel in the north and the
		// track list in the center. This way the timing panel will remain a
		// constant height whilst the track list will grow when the window is
		// resized vertically.
		setLayout(new BorderLayout());

		// Add a timing panel with a default loop timing before a project is
		// loaded.
		temp = new TimingPanel(controller, new LoopTiming(0, 0, 0, 0));
		this.add(temp, BorderLayout.NORTH);

		this.list = new TrackList(this.controller, new Project(new LoopTiming(0, 0, 0, 0), "Empty Project", null));

		// Add a scroll pane for the track list, with the vertical scrollbar
		// always visible. Add a blank track list before a project is loaded.
		JScrollPane pane = new JScrollPane(list);
		pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		pane.setPreferredSize(new Dimension(0, 600));

		add(pane, BorderLayout.CENTER);
	}

	/**
	 * Change the timing panel and track list to reflect the given project.
	 */
	public void setProject(Project project) {
		// Remove all the previous project GUI.
		this.removeAll();

		// Add a timing panel based on the new project's timing information and
		// put it in the north of the layout.
		temp = new TimingPanel(this.controller, project.getTiming());
		this.add(temp, BorderLayout.NORTH);

		// Create a new scroll pane for the track list and add it in the center
		// of the layout.
		this.list = new TrackList(this.controller, project);
		JScrollPane pane = new JScrollPane(this.list);
		pane.setPreferredSize(new Dimension(0, 600));

		// setBorder(new EmptyBorder(0, 0, 0, 0));
		// getViewport().setBackground(Color.white);
		// setPreferredSize(new Dimension(0, 600));

		add(pane, BorderLayout.CENTER);

		// this.add(new TrackList());
	}

	public void updateTiming(Project project) {
		this.remove(temp);
		temp = new TimingPanel(this.controller, project.getTiming());
		this.add(temp, BorderLayout.NORTH);
		JScrollPane pane = new JScrollPane(list);
		pane.setPreferredSize(new Dimension(0, 600));
		add(pane, BorderLayout.CENTER);
	}
	
	public void startBar() {
		temp.startBar();
	}

}
